package com.haixiaoke.saas.repository.electronicVisa.api;


import com.fasc.open.api.bean.base.BaseRes;
import com.fasc.open.api.bean.common.Actor;
import com.fasc.open.api.bean.common.ActorCorpMember;
import com.fasc.open.api.bean.common.FieldPosition;
import com.fasc.open.api.bean.common.Notification;
import com.fasc.open.api.exception.ApiException;
import com.fasc.open.api.utils.ResultUtil;
import com.fasc.open.api.v5_1.client.OpenApiClient;
import com.fasc.open.api.v5_1.client.ServiceClient;
import com.fasc.open.api.v5_1.req.signtask.AddFillFieldInfo;
import com.fasc.open.api.v5_1.req.signtask.AddSignConfigInfo;
import com.fasc.open.api.v5_1.req.signtask.AddSignFieldInfo;
import com.fasc.open.api.v5_1.res.service.AccessTokenRes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 基础类Demo
 */
@Component
public class FadadaBaseUtil {

    @Value("${electronicVisa.fadada.appId}")
    public  String APPID ;
    @Value("${electronicVisa.fadada.appSecret}")
    public  String APPSECRET;
    @Value("${electronicVisa.fadada.serverUrl}")
    public  String SERVERURL ;
    public static OpenApiClient openApiClient;

    public static String accessToken;
    /**
     * 测试数据
     */
    // 企业主体在应用中的唯一标识
    public static String clientCorpId = "";
    // 企业主体在应用上的openCorpId
    public static String openCorpId = "";
    // 企业名称
    public static String corpName = "";
    // 企业统一信用代码
    public static String corpIdentNo = "";
    // 企业印章
    public static Long corpSealId = null;
    // 企业成员Id
    public static Long memberId = null;
    // 部门Id
    public static Long deptId = null;
    // 个人用户在应用中的唯一标识
    public static String clientUserId = "";
    // 法大大平台为该用户在该应用appId范围内分配的唯一标识
    public static String openUserId = "";
    // 个人签名Id
    public static Long personSealId = null;
    // 文档模板Id
    public static String docTemplateId = null;
    // 签署模板Id
    public static String signTemplateId = "";
    // 应用模板id
    public static String appTemplateId = "";
    // 控件唯一标识，由接入方系统指定
    public static String fieldKey = "";
    // 模板控件文档序号
    public static String TemplateFieldDocId = "";
    // 文档文件Id
    public static String docFileId = "";
    // 附件文件Id
    public static String attachFileId = "";
    // 手机号
    public static String mobile = "";
    // 签署任务Id
    public static String signTaskId = "";
    // 业务场景标识，该参数是在应用管理后台创建业务场景时由法大大平台产生的。长度最大32字符
    public static String businessId = "";
    // 业务参考号，由应用基于自身业务上下文提供
    public static String transReferenceId = "";

    /**
     * 初始化客户端
     *
     * @return
     */
    public OpenApiClient getClient() {
        return new OpenApiClient(APPID, APPSECRET, SERVERURL);
    }

    /**
     * 获取应用accessToken
     *
     * @return
     * @throws Exception
     */
    public String getAccessToken(OpenApiClient openApiClient) throws ApiException {
        ServiceClient serviceClient = new ServiceClient(openApiClient);
        BaseRes<AccessTokenRes> res = serviceClient.getAccessToken();
        ResultUtil.printLog(res, openApiClient.getJsonStrategy());
        return res.getData().getAccessToken();
    }

    /**
     * 控件定位信息
     *
     * @param positionMode
     * @param positionPageNo
     * @param positionX
     * @param postionY
     * @param positionKeyword
     * @return
     */
    public static FieldPosition getFieldPosition(String positionMode, Integer positionPageNo, String positionX, String postionY, String positionKeyword) {
        FieldPosition fieldPosition = new FieldPosition();
        fieldPosition.setPositionMode(positionMode);
        fieldPosition.setPositionPageNo(positionPageNo);
        fieldPosition.setPositionX(positionX);
        fieldPosition.setPositionY(postionY);
        fieldPosition.setPositionKeyword(positionKeyword);
        return fieldPosition;
    }

    /**
     * 构造参与方对象信息
     *
     * @param actorId
     * @param actorType
     * @param actorName
     * @param permissions
     * @param actorOpenId
     * @param actorFDDId
     * @param actorCorpMembers
     * @param identNameForMatch
     * @param certNoForMatch
     * @param notification
     * @return
     * @throws ApiException
     */
    public static Actor getActor(String actorId, String actorType, String actorName, String[] permissions,
                                 String actorOpenId, String actorFDDId, ActorCorpMember[] actorCorpMembers,
                                 String identNameForMatch, String certNoForMatch, Notification notification) throws ApiException {
        Actor actor = new Actor();
        actor.setActorId(actorId);
        actor.setActorType(actorType);
        actor.setActorName(actorName);
        if (permissions != null) {
            actor.setPermissions(Arrays.asList(permissions));
        }
        actor.setActorOpenId(actorOpenId);
        actor.setActorFDDId(actorFDDId);
        if (actorCorpMembers != null) {
            actor.setActorCorpMembers(Arrays.asList(actorCorpMembers));
        }
        actor.setIdentNameForMatch(identNameForMatch);
        actor.setCertNoForMatch(certNoForMatch);
        actor.setSendNotification(false);
        return actor;
    }

    /**
     * 构造参与方-填写控件列表
     *
     * @param fieldDocId
     * @param fieldId
     * @param fieldName
     * @param fieldValue
     * @return
     */
    public static AddFillFieldInfo getAddFillFieldInfo(String fieldDocId, String fieldId, String fieldName, String fieldValue) {
        AddFillFieldInfo addFillFieldInfo = new AddFillFieldInfo();
        addFillFieldInfo.setFieldDocId(fieldDocId);
        addFillFieldInfo.setFieldId(fieldId);
        addFillFieldInfo.setFieldName(fieldName);
        addFillFieldInfo.setFieldValue(fieldValue);
        return addFillFieldInfo;
    }

    /**
     * 构造参与方-签署控件列表
     *
     * @param fieldDocId
     * @param fieldId
     * @param fieldName
     * @return
     */
    public static AddSignFieldInfo getAddSignFieldInfo(String fieldDocId, String fieldId, String fieldName, Long sealId, Boolean moveable) {
        AddSignFieldInfo addSignFieldInfo = new AddSignFieldInfo();
        addSignFieldInfo.setFieldDocId(fieldDocId);
        addSignFieldInfo.setFieldId(fieldId);
        addSignFieldInfo.setFieldName(fieldName);
        addSignFieldInfo.setSealId(sealId);
        addSignFieldInfo.setMoveable(moveable);
        return addSignFieldInfo;
    }

    /**
     * 构造参与方-签署配置信息
     *
     * @param orderNo
     * @param blockHere
     * @param requestVerifyFree
     * @param signerSignMethod
     * @return
     */
    public static AddSignConfigInfo getAddSignConfigInfo(int orderNo, boolean blockHere, boolean requestVerifyFree, String signerSignMethod, Boolean identifiedView,
                                                         List<String> verifyMethods, Boolean joinByLink, Boolean readingToEnd, String readingTime, Boolean requestMemberSign) {
        AddSignConfigInfo addSignConfigInfo = new AddSignConfigInfo();
        addSignConfigInfo.setVerifyMethods(verifyMethods);
        addSignConfigInfo.setJoinByLink(joinByLink);
        addSignConfigInfo.setReadingToEnd(readingToEnd);
        addSignConfigInfo.setReadingTime(readingTime);
        addSignConfigInfo.setOrderNo(orderNo);
        addSignConfigInfo.setBlockHere(blockHere);
        addSignConfigInfo.setRequestVerifyFree(requestVerifyFree);
        addSignConfigInfo.setSignerSignMethod(signerSignMethod);
        addSignConfigInfo.setIdentifiedView(identifiedView);
        addSignConfigInfo.setRequestMemberSign(requestMemberSign);
        return addSignConfigInfo;
    }
}
